package com.acme.bankapp.domain.account;

import com.acme.bankapp.exceptions.*;

public class CheckingAccount extends AbstractAccount {
    private double overdraft;

    public double getOverdraft() {
        return overdraft;
    }

    public CheckingAccount(final double amount, final double overdraft) throws IllegalArgumentException {
        super(amount);
        if (overdraft < 0) {
            throw new IllegalArgumentException("CheckingAccount: Overdraft can not be negative");
        }
        this.overdraft = overdraft;
    }

    @Override
    public void withdraw(final double amount) throws OverDraftLimitExceededException {
        if(getBalance() + overdraft >= amount){
            setBalance(getBalance() - amount);
        }
        else {
            throw new OverDraftLimitExceededException(this);
        }
    }

    @Override
    public double maximumAmountToWithdraw() {
        return getBalance() + overdraft;
    }

    @Override
    public String toString() {
        return "CheckingAccount{balance="+getBalance()+"; overdraft=" + getOverdraft() +"}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CheckingAccount that = (CheckingAccount) o;

        // Comparison of business keys (id) enough here
        if (this.getId() != that.getId()) return false;
        // if (Double.compare(that.getBalance(), this.getBalance()) != 0) return false;
        // if (Double.compare(that.getOverdraft(), this.getOverdraft()) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long temp = 31*super.hashCode() + overdraft != +0.0d ? Double.doubleToLongBits(overdraft) : 0L;
        return (int) (temp ^ (temp >>> 32));
    }

    public AccountType getAccountType() {
        return  AccountType.CHECKING;
    }
}
